% This file reproduces the calibration of Table 2 and the age profile of Figure 7

clear all
close all

root="/Users/luigipaciello/Dropbox/RESEARCH/1 PUBLISHED PAPERS/JPE2025/ReproductionMatlab";


str1="/calibration_data/";
path_load=append(root,str1);
addpath(path_load)
str2="/results/";
path_save=append(root,str2);
addpath(path_save)
str_outcome = "_calibration";
str3="/codes/";
path_codes=append(root,str3);
addpath(path_codes)
calib = 1; %tell the code to change the grid for the new calibration. 
do_event_study=0;

get_params;

%calibration south
south   = 1;
get_calibration_region;
%calibration north
south   = 0;
get_calibration_region;

%% %%%%%% OBTAIN FIGURE 7

str1="profile_";
str2="_all";
str_region="South";
str_expe="_calibration";
str_save=append(path_save,str1,str_region,str2,str_expe,".mat");
filename=cell2mat(str_save);
load(filename)

str1="params_";
str2="_1";
str_save=append(path_save,str1,str_region,str2,str_expe,".mat");
filename=cell2mat(str_save);
load(filename)
b_surv_s=b_surv_mat_all;
bankruptcy_yearly_s=exit_prob_mat_all;
exit_prob_yearly_s=exit_prob_mat_all+delta;
z_surv_s=z_surv_mat_all;

str1="profile_";
str2="_all";
str_region="North";
str_expe="_calibration";
str_save=append(path_save,str1,str_region,str2,str_expe,".mat");
filename=cell2mat(str_save);
load(filename)
str1="params_";
str2="_1";
str_save=append(path_save,str1,str_region,str2,str_expe,".mat");
filename=cell2mat(str_save);
load(filename)

b_surv_n=b_surv_mat_all;
bankruptcy_yearly_n=exit_prob_mat_all;
exit_prob_yearly_n=exit_prob_mat_all+delta;
z_surv_n=z_surv_mat_all;

weight_s = wei_s;
weight_n = 1-wei_s;

time_irf=0:14;
figure(30)
plot(time_irf,weight_n*bankruptcy_yearly_n(1:15)+weight_s*bankruptcy_yearly_s(1:15),'b-','LineWidth',6),hold on
plot(time_irf,weight_n*bankrpcy_targ_north(1:15)+weight_s*bankrpcy_targ_south(1:15),'r--','LineWidth',6)
le=legend('Model','Data');set(le,'Interpreter','Latex','Fontsize',16);
axis([0,14,0,.15])
set(gca, 'ytick', 0:0.15/10:0.15);settt
xlabel('Year','Fontsize',18,'Interpreter','Latex')
ylabel('Exit with bankruptcy','Fontsize',18,'Interpreter','Latex')
str1="/figures/figure_30.pdf";
str_save1=append(root,str1);
grid on
print(gcf,'-dpdf',str_save1)

figure(31)
plot(time_irf,bankruptcy_yearly_n(1:15)-bankruptcy_yearly_s(1:15),'b-','LineWidth',6),hold on
plot(time_irf,bankrpcy_targ_north(1:15)-bankrpcy_targ_south(1:15),'r--','LineWidth',6)
axis([0,14,-0.05,.05])
set(gca, 'ytick', -0.05:0.1/10:.05);settt
le=legend('Model','Data');
set(le,'Interpreter','Latex','Fontsize',16);
xlabel('Year','Fontsize',18,'Interpreter','Latex')
ylabel('Absolute change','Fontsize',18,'Interpreter','Latex')
str1="/figures/figure_31.pdf";
str_save1=append(root,str1);
grid on
settt
print(gcf,'-dpdf',str_save1)

figure(32)
plot(time_irf,weight_s*[b_surv_n(1:15)]+weight_n*[b_surv_s(1:15)],'-b','LineWidth',6),hold on
plot(time_irf,weight_s*b_targ_north(1:15)+weight_n*b_targ_south(1:15),'--r','LineWidth',6),hold off
le=legend('Model','Data');
set(le,'Interpreter','Latex','Fontsize',16);
xlabel('Year','Fontsize',18,'Interpreter','Latex')
ylabel('Leverage ratio','Fontsize',18,'Interpreter','Latex')
settt
grid on
axis([0,14,1,3.0])
set(gca, 'ytick', 1:2/10:3);settt
str1="/figures/figure_32.pdf";
str_save1=append(root,str1);
grid on
settt
print(gcf,'-dpdf',str_save1)

figure(33)
plot(time_irf,[b_surv_n(1:15)]-[b_surv_s(1:15)],'-b','LineWidth',6),hold on
plot(time_irf,b_targ_north(1:15)-b_targ_south(1:15),'--r','LineWidth',6),hold off
xlabel('Year','Fontsize',18,'Interpreter','Latex')
ylabel('Absolute change','Fontsize',18,'Interpreter','Latex')
le=legend('Model','Data');
set(le,'Interpreter','Latex','Fontsize',16);
axis([0,14,-0.1,0.7])
set(gca, 'ytick', -0.1:0.8/10:.7);settt
str1="/figures/figure_33.pdf";
str_save1=append(root,str1);
grid on
settt
print(gcf,'-dpdf',str_save1)

time_irf=0:14;
figure(37)
plot(time_irf,weight_n*exit_prob_yearly_n(1:15)+weight_s*exit_prob_yearly_s(1:15),'b-','LineWidth',6),hold on
plot(time_irf,weight_n*exit_targ_north(1:15)+weight_s*exit_targ_south(1:15),'r--','LineWidth',6)
le=legend('Model','Data');
set(le,'Interpreter','Latex','Fontsize',16);
axis([0,14,0,.15])
set(gca, 'ytick', 0:0.15/10:0.15);settt
xlabel('Year','Fontsize',18,'Interpreter','Latex')
ylabel('Exity probability','Fontsize',18,'Interpreter','Latex')
str1="/figures/figure_exit_avg.pdf";
str_save1=append(root,str1);
grid on
settt
print(gcf,'-dpdf',str_save1)

figure(38)
plot(time_irf,exit_prob_yearly_n(1:15)-exit_prob_yearly_s(1:15),'b-','LineWidth',6),hold on
plot(time_irf,exit_targ_north(1:15)-exit_targ_south(1:15),'r--','LineWidth',6)
axis([0,14,-0.05,.05])
set(gca, 'ytick', -0.05:0.1/10:.05);settt
le=legend('Model','Data');
set(le,'Interpreter','Latex','Fontsize',16);
xlabel('Year','Fontsize',18,'Interpreter','Latex')
ylabel('Absolute change','Fontsize',18,'Interpreter','Latex')
str1="/figures/figure_exit_diff.pdf";
str_save1=append(root,str1);
grid on
settt
print(gcf,'-dpdf',str_save1)
